package com.lion.appletrip;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.VolleyError;
import com.lion.appletrip.model.GridView.User;
import com.lion.appletrip.utils.ApiClient;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinbo.utils.UILUtils;
import com.xinbo.utils.VolleyListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class GridViewAllActivity extends Activity
{
	private List<com.lion.appletrip.model.GridView.Datum> gridList = new ArrayList<com.lion.appletrip.model.GridView.Datum>();
	private GridAdapter adapter;
	private GridView mGridView;
	private View btnBack;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grid_view_all);
		mGridView = (GridView) findViewById(R.id.gridView2046);
		btnBack = findViewById(R.id.btn_back_all);
		adapter = new GridAdapter();
		initData();
		mGridView.setAdapter(adapter);
	}
	private void initData()
    {
	    ApiClient.gridView_all(this,new VolleyListener()
		{
			
			@Override
			public void onResponse(String json)
			{
				jsonToText(json);
				
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0)
			{
				// TODO Auto-generated method stub
				
			}
		});
	    
    }
	protected void jsonToText(String json)
    {
		gridList.clear();
		JSONObject jsonObject;
        try
        {
	        jsonObject = new JSONObject(json);
	        JSONObject data = jsonObject.getJSONObject("data");
	        JSONArray hotSpotlList = data.getJSONArray("hot_spot_list");
	        for (int i = 0; i < hotSpotlList.length(); i++)
            {
	        	JSONObject gridData = hotSpotlList.getJSONObject(i);
	        	String text = gridData.getString("text");
				boolean isLiked = gridData.getBoolean("is_liked");
				String indexCover = gridData.getString("index_cover");
				String coverImageS = gridData.getString("cover_image_s");
				boolean isHidingLocation = gridData
				        .getBoolean("is_hiding_location");
				long spotId = gridData.getLong("spot_id");
				// user 的json解析数据
				JSONObject user = gridData.getJSONObject("user");
				String locationName = user.getString("location_name");
				String name = user.getString("name");
				String avatarS = user.getString("avatar_s");
				User user2 = new User(locationName, name, avatarS);
				// Region的json解析数据
				/*
				 * JSONObject region = gridData.getJSONObject("region");
				 * String primary = region.getString("primary"); String
				 * secondary = region.getString("secondary"); Region region2
				 * = new Region(secondary, secondary);
				 */
				com.lion.appletrip.model.GridView.Datum userdata = new com.lion.appletrip.model.GridView.Datum(
				        text, isLiked, indexCover, coverImageS,
				        isHidingLocation, user2, spotId);
				gridList.add(userdata);
				userdata = null;
				user2 = null;
            }
	        adapter.notifyDataSetChanged();
        } catch (JSONException e)
        {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
        mGridView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id)
			{
				com.lion.appletrip.model.GridView.Datum datum = gridList
				        .get(position);
				Long spotId = datum.getSpotId();
				Intent intent = new Intent();
				intent.putExtra("spotId", spotId);
				intent.setClass(GridViewAllActivity.this, GridViewShareActivity.class);
				startActivity(intent);
			}
		});
        btnBack.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				adapter=null;
				finish();
			}
		});
    }
	class GridAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return gridList.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{

			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = null;
			ViewHolder viewHolder = null;
			if (convertView == null)
			{
				viewHolder = new ViewHolder();
				layout = getLayoutInflater().inflate(
				        R.layout.fragment_fragment1_gridview_item, null);
				viewHolder.indexCover = (ImageView) layout
				        .findViewById(R.id.iv_index_cover);
				viewHolder.tvlicationName = (TextView) layout
				        .findViewById(R.id.tv_location_name);
				viewHolder.tvContent = (TextView) layout
				        .findViewById(R.id.tv_content);
				viewHolder.avatar = (ImageView) layout
				        .findViewById(R.id.iv_avatar_s);
				viewHolder.tvNick = (TextView) layout
				        .findViewById(R.id.tv_nickname);
				layout.setTag(viewHolder);
			} else
			{
				layout = convertView;
				viewHolder = (ViewHolder) layout.getTag();
			}
			com.lion.appletrip.model.GridView.Datum datum = gridList
			        .get(position);
			UILUtils.displayImageNoAnim(datum.getIndexCover(),
			        viewHolder.indexCover);
			viewHolder.tvlicationName
			        .setText(datum.getUser().getLocationName());
			viewHolder.tvContent.setText(datum.getText());
			viewHolder.tvNick.setText(datum.getUser().getName());
			UILUtils.displayImageNoAnim(datum.getUser().getAvatarS(),
			        viewHolder.avatar);
			return layout;
		}

	}

	class ViewHolder
	{
		ImageView indexCover;
		TextView tvlicationName;
		TextView tvContent;
		ImageView avatar;
		TextView tvNick;
	}
	@Override
	protected void onDestroy()
	{
		ImageLoader.getInstance().clearMemoryCache();
	    super.onDestroy();
	}
}
